/**
 * Created by Administrator on 2017/12/11.
 */
import {
    queryGoods, addGoods, modGoods, delGoods, checkGoodsSn, querySellerShop, queryGoodsPhoto,
    queryGoodsSingle, queryGoodComments, queryMidComments, queryNativeComments, modGoodsCat, modGoodsGI, modGoodsSI,
    modGoodsAttr,queryShopList,changeOnsale
} from '../services/goods';
export default {
    namespace:'goodsManage',
    state:{
        step: {
        },
        loading:true,
        stepFormSubmitting: false,
        data:{
            list:[],
            pagination:{},
        }
    },
    effects:{
        * fetch({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(queryGoods,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            })
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            })
            if (callback) callback(response);
        },

        *queryShopList({payload, callback}, {put, call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(queryShopList,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            })
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            })
            if (callback) callback(response);
        },

        * add({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(addGoods,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            })
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            })
            if (callback) callback(response);
        },
        * mod({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(modGoods,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            });
            if (callback) callback(response);
        },
        * changeOnsale({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(changeOnsale,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            });
            if (callback) callback(response);
        },
        * modGoodsCat({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(modGoodsCat,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            });
            if (callback) callback(response);
        },
        * modGoodsGI({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(modGoodsGI,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            });
            if (callback) callback(response);
        },
        * modGoodsSI({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(modGoodsSI,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            });
            if (callback) callback(response);
        },
        * modGoodsAttr({payload, callback},{put,call}){
            yield put({
                type:'GoodsManageLoading',
                payload:true,
            });
            const response=yield call(modGoodsAttr,payload);
            yield  put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type:'GoodsManageLoading',
                payload:false,
            });
            if (callback) callback(response);
        },
        * del({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(delGoods, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },

        * keepData({payload, callback},{put,call}){
            yield put({
                type:'saveFormData',
                payload:true,
            });
            yield put({
                type:'saveFormData',
                payload:payload,
            });
            if (callback) callback(true);
        },
        * goodsSn({payload, callback},{put,call}){
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(checkGoodsSn, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * sellerShop({payload, callback},{put,call}){
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(querySellerShop, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        *singleGoods({payload, callback}, {call, put}) {
            yield put({
                type: 'GoodsManageLoading',
                payload: true,
            });
            const response = yield call(queryGoodsSingle, payload);
            yield put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type: 'GoodsManageLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        *goodComments({payload, callback}, {call, put}) {
            yield put({
                type: 'GoodsManageLoading',
                payload: true,
            });
            const response = yield call(queryGoodComments, payload);
            yield put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type: 'GoodsManageLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        *midComments({payload, callback}, {call, put}) {
            yield put({
                type: 'GoodsManageLoading',
                payload: true,
            });
            const response = yield call(queryMidComments, payload);
            yield put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type: 'GoodsManageLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        *negativeComments({payload, callback}, {call, put}) {
            yield put({
                type: 'GoodsManageLoading',
                payload: true,
            });
            const response = yield call(queryNativeComments, payload);
            yield put({
                type:'handleGoodsManage',
                payload:response,
            });
            yield put({
                type: 'GoodsManageLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * queryGoodsPhoto({payload, callback},{put,call}){
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryGoodsPhoto, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        }
    },
    reducers:{
        saveFormData(state, { payload }) {
            return {
                ...state,
                step: {
                    ...state.step,
                    ...payload,
                },
                loading:payload
            };
        },
        GoodsManageLoading(state,{payload}){
            return ({
                ...state,
                loading:payload,
            })
        },
        handleGoodsManage(state,{payload}){
            return ({
                ...state,
                data:payload.resultData.goodsManage,
            })
        },
    }
}